//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System.Windows.Forms;
using GuidanceExplorer.Model;
using GuidanceExplorer.Model.Conditions;

namespace GuidanceExplorer.Controls
{
    /// <summary>
    /// The node for a single search view.
    /// </summary>
    //[MenuItemAttribute(560, "context\\Save View as...", GuidanceExplorer.Commands.SaveLibraryAs, Keys.None)]
    [MenuItemAttribute(565, "context\\Export as Library...", GuidanceExplorer.Commands.ExportViewAsLibrary, typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(570, "context\\-", null, typeof(ModelItemMenuItemIsEditableCondition), typeof(NotGenericMenuItemCondition<MenuItemUnderSolutionNodeCondition>), Keys.None)]
    [MenuItemAttribute(580, "context\\Rename View", GuidanceExplorer.Commands.RenameView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    [MenuItemAttribute(590, "context\\Delete View", GuidanceExplorer.Commands.DeleteSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    //[MenuItemAttribute(591, "context\\Edit View...", GuidanceExplorer.Commands.NewSearchView, typeof(ModelItemMenuItemIsEditableCondition), Keys.None)]
    public class SearchViewNode : ModelNode<GuidanceExplorer.Model.SearchView>
    {
        /// <summary>
        /// Overidable value for isEditable
        /// </summary>
        private bool _isEditable = true;

        /// <summary>
        /// Factory.
        /// </summary>
        /// <returns></returns>
        public static SearchViewNode New()
        {
            return new SearchViewNode();
        }

        /// <summary>
        /// Default constructor.
        /// </summary>
        private SearchViewNode() : base("<a search view>", 5, 5) { }

        /// <summary>
        /// Whether the node can be dropped.
        /// </summary>
        public override bool CanDrop(IModelItem modelNode)
        {
            return false;
        }

        /// <summary>
        /// Whether the node is editable.
        /// </summary>
        public override bool IsEditable
        {
            get
            {
                if (_isEditable == false)
                {
                    return _isEditable;
                }

                Library library = ModelInstance.GetContainerLibrary();
                if (library != null)
                {
                    return (library.ReadProtection == ReadProtection.ReadWrite);
                }

                return true;
            }
            set
            {
                _isEditable = value;
            }

        }

        public SearchViewNode Clone(SearchView newModelInstance)
        {
            SearchViewNode cloneNode = new SearchViewNode();

            cloneNode.Text = Text;
            if (newModelInstance == null)
            {
                cloneNode.ModelInstance = (SearchView)ModelInstance.Clone();
            }
            else
            {
                cloneNode.ModelInstance = newModelInstance;
            }

			if (Nodes.Count > 0)
			{
				foreach (TreeNode tempNode in Nodes)
				{
					GuidanceItemNode tempGuiItemNode = tempNode as GuidanceItemNode;
					TreeNode copyNode = null;

					if (tempGuiItemNode != null)
					{
						GuidanceItemFileReference ItemFileReference = tempGuiItemNode.ModelInstance.Clone();
						copyNode = (TreeNode)tempGuiItemNode.Clone(ItemFileReference);
					}

					cloneNode.Nodes.Add(copyNode);
				}
			}

			            
			cloneNode.SelectedItem = SelectedItem;

            return cloneNode;
        }

        public ViewNode CloneAsView(SearchView newModelInstance)
        {
            ViewNode cloneNode = ViewNode.New();

            cloneNode.Text = Text;
            if (newModelInstance == null)
            {
                cloneNode.ModelInstance = ModelInstance.CloneAsView();
            }
            else
            {
                cloneNode.ModelInstance = newModelInstance.CloneAsView();
            }
            cloneNode.SelectedItem = SelectedItem;



            return cloneNode;
        }

    }
}

